<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="com.translation.dao.Selector"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.SQLException"%>
<%@page import="com.translation.dao.BaseDAO"%>
<%@page import="com.translation.util.Util"%>
<%@page import="java.io.IOException"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Verbs List</title>
</head>
<%!
private BaseDAO dao = new BaseDAO();

%>
<body>
<%
        final String search = Util.toNull(request.getParameter("search"));
        final String word_id = Util.toNull(request.getParameter("word_id"));
        final String locale_id = Util.toNull((String) session.getAttribute("locale_id"));
        final Integer row = Util.toInteger(request.getParameter("row"),0);
        final Integer limit = Util.toInteger(request.getParameter("limit"),50);
%>
    <form action="phrases.jsp">
        <table>
            <tr>
                <td>Search: </td>
                <td>
                    <input type="text" size="40" name="search"/>
                    <input type="hidden" size="40" name="word_id" value="<%=word_id%>"/>
                </td>
            </tr>
            <tr>
                <td>Locale: </td>
                <td>
                    <select name="locale_id">
                        <option></option>
                        <option value="es"<%= ("es".equals(locale_id)? "selected=\"selected\"":"") %>>ES</option>
                        <option value="fr"<%= ("fr".equals(locale_id)? "selected=\"selected\"":"") %>>FR</option>
                        <option value="de" <%= ("de".equals(locale_id)? "selected=\"selected\"":"") %>>DE</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td>
                    <input type="submit" name="submit"  value="Search" />
                </td>
                <td>
                    <a href="phrase_edit.jsp" >
                        Create New
                    </a>
                </td>
            </tr>
        </table>
    </form>
    <table>
        <tr>
            <th>
                Phrase
            </th>
            <th>
                Freq
            </th>
            <th>
                Button
            </th>
        </tr>
    <%
        if (word_id != null && locale_id != null) {
            final JspWriter local_out = out;
            
            Selector selector = new Selector() {
                JspWriter out = local_out;

                public void setParams(PreparedStatement st) throws SQLException,IOException {
                    int index = 1;
                    st.setString(index++, locale_id);
                    st.setString(index++, search);
                    st.setString(index++, word_id);
                    st.setInt(index++, row);
                    st.setInt(index++, limit);
                }
                
                public void row(ResultSet rs) throws SQLException,IOException {
                    String phrase_id = rs.getString("phrase_id");
                    String phrase = rs.getString("phrase");
                    String freq = rs.getString("freq");
                    %>
                    <tr>
                        <td><%=phrase%></td>
                        <td><%=freq%></td>
                        <td>
                            <a href="phrase_edit.jsp?phrase_id=<%=phrase_id%>">E</a>
                        </td>
                    </tr>
                    <%
                }
            };
            dao.selectQuery("phrases.sql", selector);
        }
    %>
        <tr>
            <td colspan="2" align="center">
                <table>
                    <tr>
                         <td>
                            <a href="phrases.jsp?search=<%=search%>&word_id=<%=word_id%>&locale_id=<%=locale_id%>&row=0&limit=<%=limit%>">0</a>
                         </td>
                        <% 
                            int index = (limit>0)? row/limit: 0;
                            int min = Math.max(1,index-5);
                            int max = min+10;

                            for (int i=min; i<max;i++) {%>
                            <td>
                                <a href="phrases.jsp?search=<%=search%>&word_id=<%=word_id%>&locale_id=<%=locale_id%>&row=<%=(i*limit)%>&limit=<%=limit%>"><%=i%></a>
                            </td>
                            <%} %>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td colspan="2" align="center">
                <select name="limit">
                    <option>10</option>
                    <option>20</option>
                    <option>50</option>
                    <option>100</option>
                    <option>200</option>
                </select>
            </td>
        </tr>
    </table>
</body>
</html>